package com.example.administrator.etrip;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
/***************************************/
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.zzu.Etrip.all_buyCar.BuyCar;
import cn.zzu.Etrip.oneday.activity.oneday_database;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener ,ViewPager.OnPageChangeListener{
    private ViewPager viewPager;//主要功能是用来左右滑动的
    private int[] imageResIds;
    private ArrayList<ImageView> imageViewList;
    private LinearLayout ll_point_container;
    private String[] contentDescs;
    private TextView tv_desc;
    private int previousSelectedPosition = 0;
    boolean isRunning = false;
    /**********************************************************************************************/
    private ImageView fivestar,fireone,firetwo ,firethree,fivestar2,meione,meitwo,meithree,meifour,meifive,meisix;
    private Button ButtonOne,ButtonTwo,ButtonThree,ButtonFour,ButtonFive,ButtonSix;
    /**********************************************************************************************/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
////////自己的代码------------------
        // 初始化布局 View视图
        initViews();

        // Model数据
        initData();

        // Controller 控制器
        initAdapter();

        //轮播图的主要功能就是开启一个线程！ 开启轮询
        new Thread() {
            public void run() {
                isRunning = true;
                while (isRunning) {
                    try {

                        //定义一个线程，控制轮播图的轮播时间
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 往下跳一位
                    ///*********Runnable方法功能，如果当前线程是UI线程就会立即执行，如果不是的话会发布到UI事件队列等待
                    runOnUiThread(new Runnable() {

                        public void run() {
                            System.out.println("设置当前位置: " + viewPager.getCurrentItem());
                            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
                        }
                    });
                }
            }

//线程的正式开启
        }.start();
        //加载自己写的代码。
        findComponentAndSetListener();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            Intent intent = new Intent(MainActivity.this,oneday_database.class);
            startActivity(intent);
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {
            Intent intent = new Intent(MainActivity.this,BuyCar.class);
            Bundle bundle = new Bundle();
            bundle.putString("date","0");
            bundle.putString("price","0");
            intent.putExtras(bundle);
            startActivity(intent);
        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    /*************************************/
    protected void onDestroy() {
        super.onDestroy();
        isRunning = false;
    }

    private void initViews() {
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setOnPageChangeListener(this);// 设置页面更新监听
//      viewPager.setOffscreenPageLimit(1);// 左右各保留几个对象
        ll_point_container = (LinearLayout) findViewById(R.id.ll_point_container);

        tv_desc = (TextView) findViewById(R.id.tv_desc);

    }
    /**
     * 初始化要显示的数据
     */
    private void initData() {
        // 图片资源id数组.*******放置在viewPaper之中
        imageResIds = new int[]{R.mipmap.localbest_lunbo_md_01, R.mipmap.localbest_lunbo_md_02, R.mipmap.localbest_lunbo_md_03, R.mipmap.oneday_sm_fangte04, R.mipmap.oneday_sm_fangte03};
        // 文本描述
        contentDescs = new String[]{
                "巩俐不低俗，我就不能低俗",
                "扑树又回来啦！再唱经典老歌引万人大合唱",
                "揭秘北京电影如何升级",
                "乐视网TV版大派送",
                "热血之都"
        };

        // 初始化要展示的5个ImageView
        imageViewList = new ArrayList<ImageView>();

        ImageView imageView;
        View pointView;
        LinearLayout.LayoutParams layoutParams;
        for (int i = 0; i < imageResIds.length; i++) {
            // 初始化要显示的图片对象
            imageView = new ImageView(this);//this 是指在当前的activity之中建立一个ImageView对象
            //自定义的ImageView建立一个自定义的的背景
            imageView.setBackgroundResource(imageResIds[i]);
            imageViewList.add(imageView);

            ///加载小白点的资源的步骤1、定义一个view 组件，设置背景资源，设置属性的匡高，

            // 加小白点, 指示器
            pointView = new View(this);
            pointView.setBackgroundResource(R.drawable.ic_launcher_background);
            //定义一个普通的布局的属性
            layoutParams = new LinearLayout.LayoutParams(5, 5);
            if (i != 0)
                //设置每两个小圆点点的距离
                layoutParams.leftMargin = 10;
            // 设置默认所有都不可用 目的是在更新适配器时候的进行使用
            pointView.setEnabled(true);
            ll_point_container.addView(pointView, layoutParams);
        }
    }

    private void initAdapter() {
        ll_point_container.getChildAt(0).setEnabled(true);
        tv_desc.setText(contentDescs[0]);
        previousSelectedPosition = 0;

        // 设置适配器
        viewPager.setAdapter(new MyAdapter());

        // 默认设置到中间的某个位置
        int pos = Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2 % imageViewList.size());
        // 2147483647 / 2 = 1073741823 - (1073741823 % 5)
        viewPager.setCurrentItem(5000000); // 设置到某个位置
    }

    class MyAdapter extends PagerAdapter {

        public int getCount() {
            return Integer.MAX_VALUE;
        }

        // 3. 指定复用的判断逻辑, 固定写法
        public boolean isViewFromObject(View view, Object object) {
//          System.out.println("isViewFromObject: "+(view == object));
            // 当划到新的条目, 又返回来, view是否可以被复用.
            // 返回判断规则
            return view == object;
        }

        // 1. 返回要显示的条目内容, 创建条目
        //创建条目对象。
        public Object instantiateItem(ViewGroup container, int position) {
            System.out.println("instantiateItem初始化: " + position);
            // container: 容器: ViewPager
            // position: 当前要显示条目的位置 0 -> 4

//          newPosition = position % 5
            //指定是哪一个条目的位置
            int newPosition = position % imageViewList.size();
            //从图片集之中提取出来加入容器
            ImageView imageView = imageViewList.get(newPosition);
            // a. 把View对象添加到container中
            container.addView(imageView);
            // b. 把View对象返回给框架, 适配器
            return imageView; // 必须重写, 否则报异常
        }

        // 2. 销毁条目
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // object 要销毁的对象
            System.out.println("destroyItem销毁: " + position);
            container.removeView((View) object);
        }
    }

    public void onPageScrolled(int position, float positionOffset,
                               int positionOffsetPixels) {
        // 滚动时调用
    }

    public void onPageSelected(int position) {
        // 新的条目被选中时调用
        System.out.println("onPageSelected: " + position);
        int newPosition = position % imageViewList.size();

        //设置文本
        tv_desc.setText(contentDescs[newPosition]);

//      for (int i = 0; i < ll_point_container.getChildCount(); i++) {
//          View childAt = ll_point_container.getChildAt(position);
//          childAt.setEnabled(position == i);
//      }
        // 把之前的禁用, 把最新的启用, 更新指示器
        ll_point_container.getChildAt(previousSelectedPosition).setEnabled(false);
        ll_point_container.getChildAt(newPosition).setEnabled(true);

        // 记录之前的位置
        previousSelectedPosition = newPosition;

    }

    public void onPageScrollStateChanged(int state) {
        // 滚动状态变化时调用
    }


    /*************************/
  public void findComponentAndSetListener(){
      /*fivestar = findViewById(R.id.fivestar);
      fivestar2 = findViewById(R.id.fivestar2);*/
      fireone = findViewById(R.id.fireone);
      firetwo = findViewById(R.id.firetwo);
      firethree = findViewById(R.id.firethree);
      meione = findViewById(R.id.meione);
      meitwo = findViewById(R.id.meitwo);
      meithree = findViewById(R.id.meithree);
      meifour = findViewById(R.id.meifour);
      meifive = findViewById(R.id.meifive);
      meisix = findViewById(R.id.meisix);
      ButtonOne = findViewById(R.id.oneDay);
      ButtonTwo = findViewById(R.id.twoday);
      ButtonThree = findViewById(R.id.specialprice);
      ButtonFour = findViewById(R.id.familytrip);
      ButtonFive = findViewById(R.id.localbest);
      ButtonSix = findViewById(R.id.othermore);
      //给每一个方法建立一个监听器
      /*fivestar.setOnClickListener(new SetListener(fivestar,R.id.fivestar));
      fivestar2.setOnClickListener(new SetListener(fivestar2,R.id.fivestar2));*/
      fireone.setOnClickListener(new SetListener(fireone,R.id.fireone));
      firetwo.setOnClickListener(new SetListener(firetwo,R.id.firetwo));
      firethree.setOnClickListener(new SetListener(firethree,R.id.firethree));
      meione.setOnClickListener(new SetListener(meione,R.id.meione));
      meitwo.setOnClickListener(new SetListener(meitwo,R.id.meitwo));
      meithree.setOnClickListener(new SetListener(meithree,R.id.meithree));
      meifour.setOnClickListener(new SetListener(meifour,R.id.meifour));
      meifive.setOnClickListener(new SetListener(meifive,R.id.meifive));
      meisix.setOnClickListener(new SetListener(meisix,R.id.meisix));
      ButtonOne.setOnClickListener(new SetListener(ButtonOne,R.id.oneDay));
      ButtonTwo.setOnClickListener(new SetListener(ButtonTwo,R.id.twoday));
      ButtonThree.setOnClickListener(new SetListener(ButtonThree,R.id.specialprice));
      ButtonFour.setOnClickListener(new SetListener(ButtonFour,R.id.familytrip));
      ButtonFive.setOnClickListener(new SetListener(ButtonFive,R.id.localbest));
      ButtonSix.setOnClickListener(new SetListener(ButtonSix,R.id.othermore));

  }
//内部类各个组件监听器
  class SetListener implements View.OnClickListener{
       int counter;
       View view;
       public SetListener(View view,int counter ){
           this.view = view;
           this.counter = counter;
       }
      public void onClick(View v) {
          switch (counter){
              /*case R.id.fivestar:
                  Intent intent = new Intent();
                  intent.setAction("");
                  startActivity(intent);
                  break;
              case R.id.fivestar2:
                  Intent intent1 = new Intent();
                  intent1.setAction("");
                  startActivity(intent1);
                  break;*/
              case R.id.fireone:
                  Intent intent2 = new Intent();
                  intent2.setAction("android.intent.action.bestSelect");
                  startActivity(intent2);
                  break;
              case R.id.firetwo:
                  Intent intent7 = new Intent();
                  intent7.setAction("android.intent.action.bestSelect");
                  startActivity(intent7);
                  break;
              case R.id.meione:
                  Intent intent3 = new Intent();
                  intent3.setAction("android.intent.action.bestSelect");
                  startActivity(intent3);
                  break;
              case R.id.firethree:
                  Intent intent4 = new Intent();
                  intent4.setAction("android.intent.action.bestSelect");
                  startActivity(intent4);
                  break;
              case R.id.meitwo:
                  Intent intent5 = new Intent();
                  intent5.setAction("android.intent.action.bestSelect");
                  startActivity(intent5);
                  break;
              case R.id.meithree:
                  Intent intent6 = new Intent();
                  intent6.setAction("android.intent.action.bestSelect");
                  startActivity(intent6);
                  break;
              case R.id.meifour:
                  Intent intent8 = new Intent();
                  intent8.setAction("android.intent.action.bestSelect");
                  startActivity(intent8);
                  break;
              case R.id.meifive:
                  Intent intent9 = new Intent();
                  intent9.setAction("android.intent.action.bestSelect");
                  startActivity(intent9);
                  break;
              case R.id.meisix:
                  Intent intent10 = new Intent();
                  intent10.setAction("android.intent.action.bestSelect");
                  startActivity(intent10);
                  break;
              case R.id.oneDay:
                  Intent intent11 = new Intent();
                  intent11.setAction("android.intent.action.Oneday");
                  startActivity(intent11);
                  break;
                  case R.id.twoday:
                  Intent intent12 = new Intent();
                  intent12.setAction("android.intent.action.twoDay");
                  startActivity(intent12);
                  break;
                  case R.id.specialprice:
                  Intent intent13 = new Intent();
                  intent13.setAction("android.intent.action.destineDay");
                  startActivity(intent13);
                  break;
              case R.id.familytrip:
                  Intent intent14 = new Intent();
                  intent14.setAction("android.intent.action.homeTrip");
                  startActivity(intent14);
                  break;
              case R.id.localbest:
                  Intent intent15 = new Intent();
                  intent15.setAction("android.intent.action.localBest");
                  startActivity(intent15);
                  break;
              case R.id.othermore:
                  Intent intent16 = new Intent();
                  intent16.setAction("android.intent.action.otherMore");
                  startActivity(intent16);
                  break;
          }
      }
  }
}
